// r.r(t), r.d(t, { loadInterface: () => pt });
var s = {
  LEGACY: "legacy",
  API: "api"
}

import SymbolList from "../SymbolList"
import TextureLoader from "../loader/TextureLoader"
import { RequestRenderTargetCommand } from "../command/render.command"
import SweepTextureLoader from "../loader/SweepTextureLoader"
import j from "../math/69626"
import z from "../math/27687"
import RenderLayers from "../utils/RenderLayers"
import { AppPhaseChangeMessage } from "../message/app.message"
import transitionTypeEnum from "../enum/transitionType.enum"
import { RemoveMattertagCommand, AddMattertagCommand, EditMattertagCommand } from "../command/mattertag.command"

import { MattertagLinkOpenedMessage } from "../message/mattertag.message"
import { NavigateToMattertagCommand } from "../command/mattertag.command"
import ModelColliderTarget from "../mesh/ModelColliderTarget"
import { NavigateToSweepCommand } from "../command/navigate.command"
import { PinClickedMessage, PinHoverChangeMessage } from "../message/pin.message"
import PinHeadMesh from "../mesh/PinHeadMesh"
import InstancedPinHeads from "../mesh/InstancedPinHeads"
import { SettingGetCommand, SettingUpdateCommand } from "../command/setting.command"
import { ToggleToolCommand } from "../command/tool.command"
import { TourEndedMessage, TourStartedMessage, TourSteppedMessage, TourStoppedMessage } from "../message/tour.message"
import Color from "../three/Color"
import Vectors from "../three/Vectors"
import { BeginSwitchViewmodeMessage, EndSwitchViewmodeMessage } from "../message/viewmode.message"

import ApplicationData from "../data/ApplicationData"
import CameraData from "../data/CameraData"
import CanvasData from "../data/CanvasData"
import FloorsData from "../data/FloorsData"
import FloorsViewData from "../data/FloorsViewData"
import LabelData from "../data/LabelData"
import MattertagsData from "../data/MattertagsData"
import MattertagsViewData from "../data/MattertagsViewData"
import MeasurementModeData from "../data/MeasurementModeData"
import ModelData from "../data/ModelData"
import RaycasterData from "../data/RaycasterData"
import RoomData from "../data/RoomData"
import SettingsData from "../data/SettingsData"
import SweepData from "../data/SweepData"
import SweepViewData from "../data/SweepViewData"
import TourData from "../data/TourData"
import ViewmodeData from "../data/ViewmodeData"
import { GetScreenPositionCommand } from "../command/screen.command"
import { ZoomInCommand, ZoomResetCommand, ZoomSetCommand } from "../command/zoom.command"
import AppPhaseModule from "../module/AppPhaseModule"
import CommonControlsModule from "../module/CommonControlsModule"
import FloorCasterModule from "../module/FloorCasterModule"
import MattertagDataModule from "../module/MattertagDataModule"
import MattertagMeshModule from "../module/MattertagMeshModule"
import MattertagViewDataModule from "../module/MattertagViewDataModule"
import ModelDataModule from "../module/ModelDataModule"
import NavigationModule from "../module/NavigationModule"
import RenderToTextureModule from "../module/RenderToTextureModule"
import { jpegAsBase64, encodeRenderTarget, getXmp } from "../utils/jpg.util"

import SphereVolume from "../volume/sphere.volume"
import BoxVolume from "../volume/box.volume"
import CylinderVolume from "../volume/cylinder.volume"
import SensorModule from "../module/SensorModule"
import SettingsModule from "../module/SettingsModule"
import ZoomControlsModule from "../module/ZoomControlsModule"
import { SourceTypeMin } from "../enum/sourceType.enum"
import * as THREE from "three"
import MeshData from "../data/MeshData"
import { ModelDataLoadedMessage, ModelLoadProgressMessage } from "../message/model.message"
import ExtObjModule from "../extobj/ExtObjModule"
import { MoveToFloorIndexCommand, ShowAllFloorsCommand, GetFloorIntersectCommand } from "../command/floor.command"
import { ChangeViewmodeCommand } from "../command/viewmode.command"
import optionsKeyEnum from "../enum/optionsKey.enum"
import { appTypeMin } from "../enum/appType.enum"
import { phaseMin, phaseState } from "../enum/phase.enum"
import sweepAlignmentEnum from "../enum/sweepAlignment.enum"
import sweepPlacementEnum from "../enum/sweepPlacement.enum"
import { TagLinkTypeMin } from "../enum/tagLinkType.enum"
import { TagDescriptionChunkType } from "../enum/tagChunkType.enum"
import { MattertagMediaType } from "../enum/tagMediaType.enum"
import viewmodeEnum from "../enum/viewmode.enum"
import modeEnum from "../enum/mode.enum"
import { ToolsList } from "../enum/tool.enum"
import PuckCollider from "../mesh/PuckCollider"
import measuringPhaseEnum from "../enum/measuringPhase.enum"
import pinTypeEnum from "../enum/pinType.enum"
import { BeginMoveToSweepMessage, EndMoveToSweepMessage } from "../message/sweeps.messge"
import { EndMoveToFloorMessage, StartMoveToFloorMessage } from "../message/floor.message"
import Engine from "../Engine"
import Sdk from "../module/SdkModule"
import ModelMeshModule from "../module/ModelMeshModule"
import SceneModule from "../module/SceneModule"
import ToursControlsModule from "../module/ToursControlsModule"
import WebGLRendererModule from "../module/WebGLRendererModule"
//引入接口

export const exportOBJ = {
  ModelLoadProgressMessage,
  RenderLayers,
  OptionsKey: optionsKeyEnum,
  Application: appTypeMin,
  AppPhase: phaseMin,
  AppPhaseChangeMessage,
  CameraTransitionType: transitionTypeEnum,
  GetFloorIntersectCommand,
  StartMoveToFloorMessage,
  EndMoveToFloorMessage,
  MoveToFloorIndexCommand,
  ShowAllFloorsCommand,
  MattertagMediaType,
  RemoveMattertagCommand,
  TagDescriptionChunkType,
  TagLinkType: TagLinkTypeMin,
  AddMattertagCommand,
  EditMattertagCommand,
  MattertagLinkOpenedMessage,
  NavigateToMattertagCommand,
  ModelDataLoadedMessage,
  ModelColliderTarget,
  NavigateToSweepCommand,
  MeasuringPhase: measuringPhaseEnum,
  SettingGetCommand: SettingGetCommand,
  SettingUpdateCommand: SettingUpdateCommand,
  PuckCollider,
  PinClickedMessage,
  PinHoverChangeMessage,
  PinHeadMesh,
  InstancedPinHeads,
  PinType: pinTypeEnum,
  SweepAlignmentType: sweepAlignmentEnum,
  SweepPlacementType: sweepPlacementEnum,
  BeginMoveToSweepMessage,
  EndMoveToSweepMessage,
  ToggleToolCommand,
  Tools: ToolsList,
  TourStartedMessage,
  TourStoppedMessage,
  TourEndedMessage,
  TourSteppedMessage,
  Color,
  Vectors,
  Viewmode: viewmodeEnum,
  CommandViewmode: modeEnum,
  ChangeViewmodeCommand,
  BeginSwitchViewmodeMessage,
  EndSwitchViewmodeMessage,
  GetScreenPositionCommand,
  ZoomInCommand,
  ZoomResetCommand,
  ZoomSetCommand,
  SourceType: SourceTypeMin,
  sensorVolumes: { sphere: SphereVolume, box: BoxVolume, cylinder: CylinderVolume }
}

export type exportFUN = {
  loadImage: typeof TextureLoader
  waitForUpdate: Engine["after"]
  subscribe: Engine["subscribe"]
  addBinding: Engine["commandBinder"]["addBinding"]
  issueCommand: Engine["commandBinder"]["issueCommand"]
  requestTarget: () => Promise<any>
  getRenderLayer: Engine["getRenderLayer"]
  jpegAsBase64: typeof jpegAsBase64
  encodeRenderTarget: typeof encodeRenderTarget
  getXmp: typeof getXmp
  getOrientedAngleTo: typeof j.k2
  screenPositionToNDC: typeof z.z5
  ndcToWorldPosition: typeof z.st
  worldToScreenPosition: typeof z.q9
}
export type exportData = {
  applicationData: Promise<ApplicationData>
  cameraData: Promise<CameraData>
  canvasData: Promise<CanvasData>
  floorData: Promise<FloorsData>
  floorViewData: Promise<FloorsViewData>
  labelData: Promise<LabelData>
  mattertagData: Promise<MattertagsData>
  mattertagViewData: Promise<MattertagsViewData>
  measurementModeData: Promise<MeasurementModeData>
  modelData: Promise<ModelData>
  raycasterData: Promise<RaycasterData>
  roomData: Promise<RoomData>
  settingsData: Promise<SettingsData>
  sweepData: Promise<SweepData>
  sweepViewData: Promise<SweepViewData>
  tourData: Promise<TourData>
  viewmodeData: Promise<ViewmodeData>
  appPhaseModule: Promise<AppPhaseModule>
  commonControlsModule: Promise<CommonControlsModule>
  floorCasterModule: Promise<FloorCasterModule>
  mattertagDataModule: Promise<MattertagDataModule>
  mattertagMeshModule: Promise<MattertagMeshModule>
  mattertagViewDataModule: Promise<MattertagViewDataModule>
  modelDataModule: Promise<ModelDataModule>
  modelMeshModule: Promise<ModelMeshModule>
  meshData: Promise<MeshData>
  navigationModule: Promise<NavigationModule>
  renderToTextureModule: Promise<RenderToTextureModule>
  sceneModule: Promise<SceneModule>
  sensorModule: Promise<SensorModule>
  settingsModule: Promise<SettingsModule>
  tourControlsModule: Promise<ToursControlsModule>
  webglRendererModule: Promise<WebGLRendererModule>
  zoomControlModule: Promise<ZoomControlsModule>
  sweepTextureLoader
  extObjModule: Promise<ExtObjModule>
}
async function loadInterface(sdk: Sdk, engine: Engine) {
  const interfaceSDK = await import(
    /* webpackChunkName: "sdk-interface" */
    "../interface/index"
  )
  const exportThree = { THREE }

  const exportFunc = exportFunction(engine)
  const sweepTextureLoader = (async () => {
    const t = (await engine.getModule(ModelDataModule)).getSignedUrls()
    return new SweepTextureLoader(t)
  })()
  const exportData: exportData = {
    applicationData: engine.market.waitForData(ApplicationData),
    cameraData: engine.market.waitForData(CameraData),
    canvasData: engine.market.waitForData(CanvasData),
    floorData: engine.market.waitForData(FloorsData),
    floorViewData: engine.market.waitForData(FloorsViewData),
    labelData: engine.market.waitForData(LabelData),
    mattertagData: engine.market.waitForData(MattertagsData),
    mattertagViewData: engine.market.waitForData(MattertagsViewData),
    measurementModeData: engine.market.waitForData(MeasurementModeData),
    modelData: engine.market.waitForData(ModelData),
    raycasterData: engine.market.waitForData(RaycasterData),
    roomData: engine.market.waitForData(RoomData),
    settingsData: engine.market.waitForData(SettingsData),
    sweepData: engine.market.waitForData(SweepData),
    sweepViewData: engine.market.waitForData(SweepViewData),
    tourData: engine.market.waitForData(TourData),
    viewmodeData: engine.market.waitForData(ViewmodeData),
    appPhaseModule: engine.getModule(AppPhaseModule),
    commonControlsModule: engine.getModule(CommonControlsModule),
    floorCasterModule: engine.getModule(FloorCasterModule),
    mattertagDataModule: engine.getModule(MattertagDataModule),
    mattertagMeshModule: engine.getModule(MattertagMeshModule),
    mattertagViewDataModule: engine.getModule(MattertagViewDataModule),
    modelDataModule: engine.getModule(ModelDataModule),
    modelMeshModule: engine.getModuleBySymbol(SymbolList.MODEL_MESH),
    meshData: engine.market.waitForData(MeshData),
    navigationModule: engine.getModule(NavigationModule),
    renderToTextureModule: engine.getModule(RenderToTextureModule),
    sceneModule: engine.getModuleBySymbol(SymbolList.SCENE),
    sensorModule: engine.getModule(SensorModule),
    settingsModule: engine.getModule(SettingsModule),
    tourControlsModule: engine.getModuleBySymbol(SymbolList.TOUR_CONTROLS),
    webglRendererModule: engine.getModuleBySymbol(SymbolList.WEBGL_RENDERER),
    zoomControlModule: engine.getModule(ZoomControlsModule),
    sweepTextureLoader,
    extObjModule: engine.getModule(ExtObjModule)
  }

  const islegacyIds = new URLSearchParams(window.location.search).get("useLegacyIds")
  const idOption = islegacyIds || s.API
  interfaceSDK.setup(sdk, exportThree, exportOBJ, exportFunc, exportData, { idOption })
}
export function exportFunction(engine: Engine) {
  return {
    loadImage: TextureLoader,
    waitForUpdate: () => engine.after(phaseState.End),
    subscribe: (t, r) => engine.subscribe(t, r),
    addBinding: (t, r) => engine.commandBinder.addBinding(t, r),
    issueCommand: t => engine.commandBinder.issueCommand(t),
    requestTarget: () => engine.commandBinder.issueCommand(new RequestRenderTargetCommand()),
    getRenderLayer: t => engine.getRenderLayer(t),
    jpegAsBase64,
    encodeRenderTarget,
    getXmp,
    getOrientedAngleTo: j.k2,
    screenPositionToNDC: z.z5,
    ndcToWorldPosition: z.st,
    worldToScreenPosition: z.q9
  }
}
export default {
  loadInterface
}
